<?php
namespace Admin\Service;

/**
 * 风险管理基础服务
 */

class RiskService
{
    public function add( $data )
    {
        $res = array('rcode'=>0,'data'=>0,'msg'=>'Err');
        if( !$data ){
            $res['msg'] = "数据非法";
            return $res;
        }
        //必填字段验证
        if( !$data['customer_name']  && !$data['customer_phone'] ){
            $res['msg'] = "风险客户信息不能为看空";
            return $res;
        }
        if( !$data['sales_id'] || !$data['sales_subcompany'] ){
            $res['msg'] = "客户关联业务人员不能为空";
            return $res;
        }
        if( !$data['description'] || !$data['feedback'] ){
            $res['msg'] = "风险详情描述与反馈不能为空";
            return $res;
        }
        //需要自动填充的字段
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['is_delete'] = 0;
        $data['status'] = \Admin\Model\RiskModel::STATUS_INIT;

        //添加进数据库
        $Mdo = D('Admin/Risk');
        $rid = $Mdo->data( $data )->add();


        if( $rid ){
            $MTrack = M('track','risk_');

            $d = array();
            $d['risk_id'] = $rid;
            $d['reply'] = $data['feedback'];
            $d['reply_time'] = date('Y-m-d H:i:s');

            $dlistTrack = $MTrack->data($d)->add();

            if($dlistTrack) {
                $res['rcode'] = 1;
                $res['data'] = $rid;
            } else {
                $res['msg'] = $Mdo->getError();
            }
        }
        else{
            $res['msg'] = $Mdo->getError();
        }
        return $res;
    }

    /**
     * 客户最近投诉记录
     * @param  int $_size 返回记录条数,FALSE 返回全部内容
     * @return mixed/array 查询返回结果
     */
    public function recent( $risk_id, $_size = FALSE )
    {
        $Mdo = D('Admin/RiskTrack');
        $wheres = array();
        $wheres['risk_id'] = array('eq', $risk_id );
        $data = FALSE;
        if( $_size ){
            $data = $Mdo->where( $wheres )->order("confirm_time desc")->limit( $_size )->select();
        }
        else{
            $data = $Mdo->where( $wheres )->order("confirm_time desc")->select();
        }
        return $data;
    }

    public function edit()
    {
        //
    }

    public function _find( $risk_id ){
        $MRisk = D('Admin/Risk');
        $wheres = array();
        $wheres['risk.risk_id'] = array('eq', $risk_id );
        $data = $MRisk->field("risk.*,sector.sector_name as company,user.nickname as manager_name,sale.nickname as sales_name,a.reply,
                        CASE WHEN risk.status = 0 THEN '未处理'
                            WHEN risk.status = 1 THEN '处理中'
                            WHEN risk.status = 2 THEN '已处理'
                            WHEN risk.status = 3 THEN '挂起'
                            ELSE '未知' END AS status_show
                        ")
                      ->join("LEFT JOIN sector on risk.sales_subcompany=sector.sector_id")
                      ->join("LEFT JOIN user on user.user_id=risk.sales_manager")
                      ->join("LEFT JOIN user sale on sale.user_id=risk.sales_id")
                      ->join("LEFT JOIN risk_track as a on a.risk_id = risk.risk_id")
                      ->where( $wheres )->find();

        return $data;
    }

    public function confirm( $risk_id, $_confirm = FALSE, $status = FALSE)
    {
        $res = array('rcode'=>0,'data'=>0,'msg'=>'500 Internal Error');
        $data = $this->_find( $risk_id );
        if( $data ){
            //查询风险数据
            if( FALSE === $_confirm ){
                if( 1== $data['is_delete'] ){
                    $res['msg'] = "数据被删除";
                    return $res;
                }
                if( \Admin\Model\RiskModel::STATUS_INIT !=$data['status'] && \Admin\Model\RiskModel::STATUS_HANDLE!=$data['status']  )
                    $res['msg'] = "风险已经确认反馈";
                else{
                    $res['rcode'] = 1;
                    $res['data'] = $data;
                }
            }
            else{
                if( 1== $data['is_delete'] ){
                    $res['msg'] = "数据被删除";
                    return $res;
                }
                if( \Admin\Model\RiskModel::STATUS_INIT !=$data['status'] && \Admin\Model\RiskModel::STATUS_HANDLE!=$data['status']  )
                    $res['msg'] = "风险已经确认反馈";
                else{
                    // $data['status'] = \Admin\Model\RiskModel::STATUS_HANDLE;
                    $data['status'] = $status;
                    $MRisk = D('Admin/Risk');
                    $wheres = array();
                    $wheres['risk_id'] = array('eq', $risk_id );
                    $ret = $MRisk->where( $wheres )->save( $data );

                    $d = array();
                    $d['reply_confirm'] = $_confirm;
                    if(\Admin\Model\RiskModel::STATUS_INIT != $status)
                        $d['confirm_time'] = date('Y-m-d');
                    $wheres = array();
                    $wheres['risk_id'] = array('eq', $risk_id );
                    $rcode = M('track','risk_')->where( $wheres )->save( $d );

                    if( $rcode ){
                        $res['rcode'] = 1;
                        $res['msg'] = '确认反馈成功';
                        $res['data'] = $rcode;
                    }
                    else{
                        $res['msg'] = '风险确认反馈失败';
                    }
                }
            }
        }
        else
            $res['msg'] = "操作数据不存在";

        return $res;
    }

    //风险管理挂起
    public function hangup( $risk_id , $b )
    {
        $res = array('rcode'=>0,'data'=>0,'msg'=>'500 Internal Error');
        $Mdo = D('Admin/Risk');
        $wheres = array();
        $wheres['risk_id'] = array('eq', $risk_id);
        $data = $this->_find( $risk_id );

        if( !$data ){
            $res['msg'] = "风险数据不存在";
        }
        else{
            if( $data['is_delete'] )
                $res['msg'] = "风险数据被删除";
            else{
                if( $b ){
                    $dup = array();
                    if( $data['status'] == \Admin\Model\RiskModel::STATUS_HANG ){
                        $res['rcode'] = 1;
                        $res['msg'] = "OK";
                        $res['data'] = 0;
                    }
                    else
                        $dup['status'] = \Admin\Model\RiskModel::STATUS_HANG;
                }
                if( $data['status'] == \Admin\Model\RiskModel::STATUS_HANG ){
                    $dup['status'] = \Admin\Model\RiskModel::STATUS_HANDLE;
                }
                else{
                    $res['msg'] =  "无法取消挂起状态";
                }

                if( !$res['rcode'] && $dup ){
                    $Mdo->where( $wheres )->save( $dup );
                    $res['rcode'] = 1;
                    $res['msg'] = 'OK';
                }
            }
        }
        return $res;
    }

    //关闭风险
    public function close( $risk_id, $b )
    {
        $res = array('rcode'=>0,'data'=>0,'msg'=>'500 Internal Error');
        $Mdo = D('Admin/Risk');
        $wheres = array();
        $wheres['risk_id'] = array('eq', $risk_id);
        $data = $this->_find( $risk_id );

        if( !$data ){
            $res['msg'] = "风险数据不存在";
        }
        else{
            if( $data['is_delete'] )
                $res['msg'] = "风险数据被删除";
            else{
                if( $b ){
                    $dup = array();
                    if( $data['status'] == \Admin\Model\RiskModel::STATUS_CLOSE ){
                        $res['rcode'] = 1;
                        $res['msg'] = "OK";
                        $res['data'] = 0;
                    }
                    else{
                        $dup['status'] = \Admin\Model\RiskModel::STATUS_CLOSE;
                        $dup['finish_time'] = date('Y-m-d H:i:s');//风险最后关闭时间
                    }
                }
                if( $data['status'] == \Admin\Model\RiskModel::STATUS_CLOSE ){
                    $dup['status'] = \Admin\Model\RiskModel::STATUS_HANDLE;
                }
                else{
                    $res['msg'] =  "无法取消关闭状态";
                }

                if( !$res['rcode'] ){
                    $Mdo->where( $wheres )->save( $dup );
                    $res['rcode'] = 1;
                    $res['msg'] = 'OK';
                }
            }
        }
        return $res;
    }

    public function remove( $_id ){
        $res = array('rcode'=>0,'data'=>0,'msg'=>'500 Internal Error');
        $Mdo = D('Admin/Risk');
        $wheres = array();
        $wheres['risk_id'] = array('eq', $_id );
        $dfind = $Mdo->where( $wehres )->find();
        if( $dfind ){
            if( $dfind['is_delete'] ){
                $res['rcode'] = 1;
                $res['data'] = 0;
                $res['msg'] = '数据已经删除';
            }else{
                $up = array();
                $up['is_delete'] = 1;
                $ret = $Mdo->where( $wheres )->save( $up );
                if( $ret ){
                    $res['rcode'] = 1;
                    $res['msg'] = '删除成功';
                    $res['data'] = $ret;
                }
                else{
                    $res['msg'] = '删除失败';
                }
            }
        }
        else{
            $res['msg'] = "删除数据不存在";
        }
        return $res;
    }
}

